/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author pipo
 */

package mainGame;

import controller.IconManager;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.event.*;
import javax.swing.JOptionPane;


public class GameEngine extends JFrame

{
    private JPanel gamePanel;
    private boolean newGame;

    
    public GameEngine(int X , int Y , String userName , int selection, boolean newGame)
    {
        super("Main Game Frame");
        controller.SoundManager.s1.loop();
        this.newGame = newGame;
        this.setPreferredSize(new Dimension(1215 , 976));
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        gamePanel = new drawPane(X, Y , userName , selection);
        this.getContentPane().add(gamePanel);
        this.pack();
        this.setVisible(true);
        IconManager.populatePokemonIconList();
        IconManager.populateCharacterIconList();

        
    }
    
    private class drawPane extends JPanel {

    private Map map;
    private Player player; 
    private String characterIconName;
    private int selection;
    
    /**
     * Creates new form GameEngine
     */
    public drawPane(int X , int Y , String userName , int selection) {
         
        if(newGame)
            this.map = new Map(X, Y , userName , selection, true);
        else
        {
            this.map = controller.SaveLoadManager.loadMap(userName);
        }
        this.selection = this.map.getSelection();
        this.characterIconName = "c"+ this.selection;
        setBackground(Color.YELLOW);
        this.addKeyListener(new DirectionListener());
        this.setFocusable(true);
        this.player = this.map.returnPlayer();

    }

    
    public void showGameOverDialog()
    {
        
    JOptionPane.showMessageDialog(this,
        "YOU ARE DEAD",
    "GAME OVER",
    JOptionPane.WARNING_MESSAGE);
        
    }

    public void waitInput(int direction)
    {
      if(direction == -1)
      {
          new menus.Exit(GameEngine.this , this.map , this.selection).setVisible(true); //Pause Menu
          
      }
      else if(direction != 0)
      {
          Trainer t;
          int obstacleCol = this.map.obstacleCollision(direction);
          int enemyCol = this.map.enemyCollision(direction);
          int trapCol = this.map.trapCollision(direction);
          boolean boundaryCol = this.map.playerHitBoundary(direction);
          repaint();   
          if(boundaryCol == false && obstacleCol == 0)
          {
              if(enemyCol != -1)
              {
                  GameEngine.this.setVisible(false);
                  t = this.map.returnTrainer(enemyCol);
                  FightManager fm = new FightManager(GameEngine.this,map, player, t , false);
                  fm.setVisible(true);

              }
              else if(trapCol != -1)
              {
                  
                  GameEngine.this.setVisible(false);
                  Trap trap = this.map.returnTrap(trapCol);
                  trap.reveal();
                  FightManager fm = new FightManager(GameEngine.this, map , player , trap , true);
                  fm.setVisible(true);
                  
              }
              else
              {
                this.map.movePlayer(direction);

              }
 
          }
          else if(obstacleCol == 2)
          {
              this.map.movePlayer(direction);              
              drown();
          }
          
          if((this.map.noOfItems() - 1)  == 0 && this.map.returnPlayer().pokemonCount() > 0)
            congratz();

      }
      

    }
    
    public void congratz()
    {
        controller.SoundManager.s1.stop();
        controller.SoundManager.s5.loop();
        JOptionPane.showMessageDialog(this,
        "Congratulations!  You have completed the game with success...\nYour final score is:  " + this.map.returnPlayer().getScore(),
        "You have beaten the game!",
        JOptionPane.WARNING_MESSAGE);
        GameEngine.this.dispose();
        new menus.MainMenu().setVisible(true);
        controller.HighScoreManager hsm = new controller.HighScoreManager();
        hsm.addScore(player.getScore(), player.getName()+ "  (Completed)");
        
    }
    
    public void drown()
    {
        controller.SoundManager.s1.stop();
        controller.SoundManager.s3.loop();
        JOptionPane.showMessageDialog(this,
    "You have drowned. Do not get into the water next time...",
    "GAME OVER!",
    JOptionPane.WARNING_MESSAGE);
        GameEngine.this.dispose();
        new menus.GameOver(this.player).setVisible(true);
        
    }
    
    
    public void paintComponent(Graphics page)
    {   
        super.paintComponent(page);
        Image mapImage = controller.IconManager.map.returnImage();
        page.drawImage(mapImage, 0, 0, null);
        page.setFont(new Font("Arial", Font.BOLD , 30));
        page.drawString("Score: " + player.getScore(), 1035 , 25);
        GameItem t;
        int X , Y; 
        int size = 45;
        t = this.player;//player
        X = t.getXCoor();
        Y = t.getYCoor();
        Image playerImage = controller.IconManager.returnCharacterImage(this.characterIconName);
        Image trapImage = controller.IconManager.returnCharacterImage("trap");
        String enemyImageName;
        page.drawImage(playerImage, X*size, Y*size, null);
        for(int i = 1 ; i < this.map.noOfItems() ; i++ )
        {
            enemyImageName = this.map.returnTrainer(i).getName();
            Image enemyImage = controller.IconManager.returnCharacterImage(enemyImageName);
            t = this.map.returnTrainer(i);
            X = t.getXCoor();
            Y = t.getYCoor();
            page.drawImage(enemyImage, X*size, Y*size, 45, 45, null);
        }
        
        for(int i = 0 ; i < this.map.noOfTraps() ; i++ )
        {
            Trap trap = this.map.returnTrap(i);
            X = trap.getXCoor();
            Y = trap.getYCoor();
            if(trap.IsRevealed()) page.drawImage(trapImage, X*size, Y*size, 45, 45, null);
        }
        
    }

    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify                     
    // End of variables declaration                   

    private class DirectionListener implements KeyListener
    {
    
        public void keyReleased(java.awt.event.KeyEvent evt) {                                 
        // TODO add your handling code here:
        int keyCode = evt.getKeyCode();
      
        if(keyCode == java.awt.event.KeyEvent.VK_UP)
        {
            waitInput(2);
        }
        else if(keyCode == java.awt.event.KeyEvent.VK_LEFT)
        {
            waitInput(1);
        }
        else if(keyCode == java.awt.event.KeyEvent.VK_RIGHT)
        {
            waitInput(3);
        }
        else if(keyCode == java.awt.event.KeyEvent.VK_DOWN)
        {
            waitInput(4);
        }
        else if(keyCode == KeyEvent.VK_ESCAPE)
          waitInput(-1); //pause menu

        else
            waitInput(0);
        }
        
        @Override
         public void keyTyped(java.awt.event.KeyEvent evt) {}
        @Override
         public void keyPressed(java.awt.event.KeyEvent evt) {}
    }
    
    
}
    
    
}







